/**
 * Created by zaranengap on 2017/7/4.
 */
import {combineReducers} from 'redux';
import {SET_AUTH_TOKEN, SET_CURRENT_PAGE, SET_LANGUAGE, SET_TENANT_MODE, SET_MENU_SORT, SET_GUIDE,
  SET_CONTROL_CONFIG,
  SET_ADMIN_MODE,
  SET_ROUTE_IS_SHOW,
  SET_PUBLIC_TOKEN,
  SET_ENABLE_SCAN,
  SET_FIRST_MENU,
  SET_CURRENT_PAGEROUTE,
} from 'actions/main'
import {cr} from 'share/utils'
import moment from 'moment';
import {getLanguageObjByCode} from 'share/utils';
let defaultLanguage = getLanguageObjByCode(null, navigator);
let localLanguage = localStorage.getItem('hly.language');
if(localLanguage && localLanguage.startsWith('"')){
  localLanguage = JSON.parse(localLanguage);
  defaultLanguage = getLanguageObjByCode(localLanguage);
} else {
  let lang;
  switch(defaultLanguage.locale){
    case 'zh':
      lang = 'zh_cn';
      break;
    case 'ja_jp':
      lang = 'ja';
      break;
    case 'zh_TW':
      lang = 'zh_TW';
      break;
    default:
      lang = 'en';
  }
  localStorage.setItem('hly.language', JSON.stringify(lang));
  sessionStorage.setItem('HLY-lang', JSON.stringify(lang));
}
document.title = defaultLanguage.messages['helios.title'];

export default combineReducers({
  currentPage: cr([{name: 'Dashboard', key:'dashboard', url:'/main'}], {
    [SET_CURRENT_PAGE](state, {currentPage}){return currentPage}
  }),
  language: cr(defaultLanguage, {
    [SET_LANGUAGE](state, {language}){
      let targetLang;
      switch(language.locale.toLowerCase()){
        case 'zh':
          targetLang = 'zh_cn';
          moment.locale('zh-cn');
          break;
        case 'ja_jp':
          targetLang = 'ja';
          moment.locale('ja');
          break;
        case 'zh_tw':
          targetLang = 'zh_TW';
          moment.locale('zh-tw');
          break;
        default:
          targetLang = 'en';
          moment.locale('en');
          break;
          //注意每一个 case 都是小写
      }
      localStorage.setItem('hly.language', JSON.stringify(targetLang));
      sessionStorage.setItem('HLY-lang', JSON.stringify(targetLang));
      document.title = language.messages['helios.title'];
      return language
    }
  }),
  tenantMode: cr(false, {
    [SET_TENANT_MODE](state, {tenantMode}){
      sessionStorage.setItem('HLY-RoleType', JSON.stringify(tenantMode ? "tenant" : "company"));
      return tenantMode
    }
  }),
  adminMode: cr(false, {
    [SET_ADMIN_MODE](state, {adminMode}){
      sessionStorage.setItem('HLY-AdminRoleType', (adminMode ? "admin" : "employee"));
      return adminMode
    }
  }),
  authToken: cr({}, {
    [SET_AUTH_TOKEN](state, {authToken}) {
      return authToken
    }
  }),
  menuSort: cr({}, {
    [SET_MENU_SORT](state, {menuSort}) {
      return menuSort
    }
  }),
  guide: cr([], {
    [SET_GUIDE](state, {guide}) {
      return guide
    }
  }),
  publicToken: cr({}, {
    [SET_PUBLIC_TOKEN](state, {publicToken}) {
      return publicToken;
    }
  }),
  controlConfig: cr({}, {
    [SET_CONTROL_CONFIG](state, {controlConfig}) {
      return controlConfig
    }
  }),
  routeIsShow: cr({}, {
    [SET_ROUTE_IS_SHOW](state, {routeIsShow}) {
      return routeIsShow
    }
  }),
  // 默认值是INIT, 2020-11.16 改成SCAN_SNAP
  enableScan: cr('INIT', {
    [SET_ENABLE_SCAN](state, {enableScan}) {
      return enableScan
    }
  }),

  firstMenu: cr({}, {
    [SET_FIRST_MENU](state, {firstMenu}) {
      return firstMenu
    }
  }),
  currentPageRoute: cr(JSON.parse(sessionStorage.getItem('HLY-CurrentPageRoute' || '{}')), {
    [SET_CURRENT_PAGEROUTE](state, {currentPageRoute}){
      sessionStorage.setItem('HLY-CurrentPageRoute', JSON.stringify(currentPageRoute));
      return currentPageRoute
    }
  }),
})
